﻿Public Class Form1

    Private Function checkInputString()
        Dim str As String = tbxInputString.Text
        If String.IsNullOrEmpty(str) Then
            MessageBox.Show("Input string is empty!")
            Return False
        Else
            For i As Integer = 0 To str.Length - 1
                If checkChar(str(i)) = False Then
                    MessageBox.Show("Input string is incorrect! Please check again!")
                    Return False
                End If
            Next

            If (str(0) = ","c) OrElse (str(str.Length - 1) = ","c) Then
                MessageBox.Show("Input string is incorrect! Please check again!")
                Return False
            End If
            Return True
        End If
    End Function

    Private Function checkChar(ByVal c As Char)
        Select Case c
            Case ","c                
                Exit Select
            Case "-"c
                Exit Select
            Case "0"c
                Exit Select
            Case "1"c
                Exit Select
            Case "2"c
                Exit Select
            Case "3"c
                Exit Select
            Case "4"c
                Exit Select
            Case "5"c
                Exit Select
            Case "6"c
                Exit Select
            Case "7"c
                Exit Select
            Case "8"c
                Exit Select
            Case "9"c
                Exit Select
            Case Else
                Return False
                Exit Select
        End Select

        Return True
    End Function

    Private Function Total()
        Dim str As String = tbxInputString.Text + ","
        Dim startPoint As Integer = 0
        Dim endPoint As Integer = 0
        Dim subStr As String = ""
        Dim iTotal As Integer = 0
        Dim iNum As Integer = 0

        For i As Integer = 0 To str.Length - 1
            If (str(i) = ","c) Then
                endPoint = i - 1
                subStr = str.Substring(startPoint, endPoint - startPoint + 1)
                iNum = Convert.ToInt32(subStr)
                iTotal = iTotal + iNum
                startPoint = i + 1
            End If            
        Next

        Return iTotal
    End Function

    Private Function Average()
        Dim str As String = tbxInputString.Text + ","
        Dim startPoint As Integer = 0
        Dim endPoint As Integer = 0
        Dim subStr As String = ""
        Dim iTotal As Integer = 0
        Dim iCount As Integer = 0
        Dim iNum As Integer = 0

        For i As Integer = 0 To str.Length - 1
            If (str(i) = ","c) Then
                endPoint = i - 1
                subStr = str.Substring(startPoint, endPoint - startPoint + 1)
                iNum = Convert.ToInt32(subStr)
                iTotal = iTotal + iNum
                iCount = iCount + 1
                startPoint = i + 1
            End If
        Next

        Return iTotal / iCount
    End Function

    Private Sub btnTotal_Click(sender As System.Object, e As System.EventArgs) Handles btnTotal.Click
        If checkInputString() Then
            MessageBox.Show(Total())
        Else

        End If
    End Sub

    Private Sub btnAverage_Click(sender As System.Object, e As System.EventArgs) Handles btnAverage.Click
        If checkInputString() Then
            MessageBox.Show(Average())
        Else

        End If
    End Sub

    Private Sub btnMin_Click(sender As System.Object, e As System.EventArgs) Handles btnMin.Click
        If checkInputString() Then
            'Do somethings
        Else

        End If
    End Sub

    Private Sub btnMax_Click(sender As System.Object, e As System.EventArgs) Handles btnMax.Click
        If checkInputString() Then
            'Do somethings
        Else

        End If
    End Sub

    Private Sub btnReverse_Click(sender As System.Object, e As System.EventArgs) Handles btnReverse.Click
        If checkInputString() Then
            'Do somethings
        Else

        End If
    End Sub

    Private Sub btnStatistics_Click(sender As System.Object, e As System.EventArgs) Handles btnStatistics.Click
        If checkInputString() Then
            'Do somethings
        Else

        End If
    End Sub
End Class
